package com.example.androidmainshixun.view;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.SectionsPagerAdapter;
import com.example.androidmainshixun.base.BaseFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends BaseFragment {
   private SectionsPagerAdapter sectionsPagerAdapter;
   private ViewPager viewPager;
   private TabLayout tabLayout;
   private EditText mgo_to_searsh;
   private Button mgo_to_sell;
   private Title0Fragment mtitle0Fragment;
   private FragmentManager mFragmentManager;
   @Override
   protected int getLayoutId() {
      // 返回 FirstFragment 布局的 ID
      return R.layout.fragment_home;
   }

   @Override
   protected void initView() {
      mgo_to_searsh=mView.findViewById(R.id.go_to_searsh);
      mgo_to_sell=mView.findViewById(R.id.go_to_sell);
      tabLayout = mView.findViewById(R.id.tabLayout);
      viewPager = mView.findViewById(R.id.view_pager);
      mFragmentManager = getChildFragmentManager();

      mtitle0Fragment = new Title0Fragment();
      List<Fragment> fragments = new ArrayList<>();
      List<String> titles = new ArrayList<>();
      fragments.add(new Title0Fragment());
      fragments.add(new Title1Fragment());
      fragments.add(new Title2Fragment());
      fragments.add(new Title3Fragment());
      titles.add("推荐");
      titles.add("水果蔬菜");
      titles.add("米面粮油");
      titles.add("农资农具");

      sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(), fragments, titles);
      viewPager.setAdapter(sectionsPagerAdapter);

      // 关联 TabLayout 和 ViewPager
      tabLayout.setupWithViewPager(viewPager);
      tabLayout.getTabAt(0).select(); // 默认选中第一个Tab

   }
   @Override
   protected void initListener() {
      mgo_to_searsh.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent =new Intent(getContext(),SearchActivity.class);
            startActivity(intent);
         }
      });
      mgo_to_sell.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent =new Intent(getContext(), SellerHomepage.class);
            startActivity(intent);
         }
      });
      tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
         @Override
         public void onTabSelected(TabLayout.Tab tab) {
            // 当Tab被选中时调用
            Fragment fragment = null;
            switch (tab.getPosition()) {
               case 0:
                  // 如果用户点击的是第一个Tab，并且mtitle0Fragment不为空，则显示mtitle0Fragment
                  if (mtitle0Fragment == null) {
                     mtitle0Fragment = (Title0Fragment) sectionsPagerAdapter.getItem(0);
                     fragment = mtitle0Fragment;
                  }
                  break;
               case 1:
                  fragment = new Title1Fragment();
                  break;
               case 2:
                  fragment = new Title2Fragment();
                  break;
               case 3:
                  fragment = new Title3Fragment();
                  break;
            }
            if (fragment != null) {
               // 替换fragment_container中的Fragment
               replaceFragment(fragment, "TAG_" + tab.getPosition());
            }
         }

         @Override
         public void onTabUnselected(TabLayout.Tab tab) {
            // 可以留空
         }

         @Override
         public void onTabReselected(TabLayout.Tab tab) {
            // 可以留空
         }
      });
   }

   private void replaceFragment(Fragment fragment, String tag) {
      mFragmentManager = getChildFragmentManager();
      mFragmentManager.beginTransaction()
              .replace(R.id.fragment_container, fragment,tag) // 使用 fragment_container ID
              .commit();
   }
}